#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    serial1 = new QSerialPort() ;
    timer = new QTimer();
    ui->SetPulse->setDisabled(not ConnectFlag);
    ui->GoSteps->setDisabled(not ConnectFlag);
    ui->Go->setDisabled(not ConnectFlag);
    QObject::connect(timer,SIGNAL(timeout()),this,SLOT(timerSlot()));

    refreshSerial();

}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_Connect_clicked()
{
    if(ConnectFlag)
    {
        serial1->close();
        ui->Connect->setText("Connect");
        ConnectFlag = false;
        ui->SetPulse->setDisabled(not ConnectFlag);
        ui->GoSteps->setDisabled(not ConnectFlag);
        ui->Go->setDisabled(not ConnectFlag);
    }
    else
    {
        initSerialPort();
        ui->Connect->setText("DisConnect");
    }
}
void MainWindow:: initSerialPort()
{
#if 0
    // 获取可用的串口列表
    QList<QSerialPortInfo> portList = QSerialPortInfo::availablePorts();

    // 如果没有可用的串口，输出错误信息
    if (portList.isEmpty()) {
        qWarning() << "No available serial port found!";
        return;
    }
    // 选择第一个可用的串口
    QSerialPortInfo portInfo = portList.first();
    qDebug() << "Using serial port: " << portInfo.portName();
#endif
    // 设置串口的参数
    serial1->setPortName(ui->comboBox->currentText());
    serial1->setBaudRate(QSerialPort::Baud115200);
    serial1->setDataBits(QSerialPort::Data8);
    serial1->setParity(QSerialPort::NoParity);
    serial1->setStopBits(QSerialPort::OneStop);

    // 打开串口
    if (!serial1->open(QIODevice::ReadWrite)) {
        qWarning() << "Failed to open serial port!";
        ConnectFlag = false;
        ui->SetPulse->setDisabled(not ConnectFlag);
        ui->GoSteps->setDisabled(not ConnectFlag);
        ui->Go->setDisabled(not ConnectFlag);
        return;
    }
    else
    {
        ConnectFlag = true;
        ui->SetPulse->setDisabled(not ConnectFlag);
        ui->GoSteps->setDisabled(not ConnectFlag);
        ui->Go->setDisabled(not ConnectFlag);
    }
#if 0
    // 连接串口的readyRead信号，用于读取串口数据
    QObject::connect(&serialPort, &QSerialPort::readyRead, [](){
        QByteArray data = serialPort.readAll();
        qDebug() << "Received data: " << data;
    });
#endif
}

void MainWindow::on_SetPulse_clicked()
{
    int value = ui->spinBox_2->value();
    uchar checksum = 0;
    Trans[0] = 0xaa;
    Trans[1] = 0xbb;
    Trans[2] = 0x0b;
    Trans[3] = 0x02;
    Trans[4] = (value >> 24) & 0xff;
    Trans[5] = (value >> 16) & 0xff;
    Trans[6] = (value >> 8) & 0xff;
    Trans[7] = (value >> 0) & 0xff;
    Trans[8] = 0x0;
    Trans[9] = 0xcc;
    Trans[10] = 0xdd;
    checksum = Checksum(Trans,Trans[2]);
    Trans[8] = checksum;
    serial1->write(reinterpret_cast<char*>(Trans),Trans[2]);

}
uchar MainWindow::Checksum(uchar * arr,int len)
{
    arr[len-3] = 0;
    uchar sum = 0x00;
    for(int i=0;i<len;i++)
    {
        sum += *(arr+i);
    }
    return sum;
}

void MainWindow::on_GoSteps_clicked()
{
    int value = ui->spinBox->value();
    uchar dir = 0;
    if(ui->DIR0->isChecked())
    {
        dir = 0;
    }
    else
    {
        dir = 1;
    }
    uchar checksum = 0;
    Trans[0] = 0xaa;
    Trans[1] = 0xbb;
    Trans[2] = 0x0c;
    Trans[3] = 0x01;
    Trans[4] = dir;
    Trans[5] = (value >> 24) & 0xff;
    Trans[6] = (value >> 16) & 0xff;
    Trans[7] = (value >> 8) & 0xff;
    Trans[8] = (value >> 0) & 0xff;
    Trans[9] = 0x0;
    Trans[10] = 0xcc;
    Trans[11] = 0xdd;
    checksum = Checksum(Trans,Trans[2]);
    Trans[9] = checksum;
    serial1->write(reinterpret_cast<char*>(Trans),Trans[2]);
}


void MainWindow::on_Go_clicked()
{
    if(timerRun)
    {
        ui->Go->setText("Go");
        timer->stop();
        timerRun = false;
    }
    else
    {
        timer->start(10);
        timerRun = true;
        ui->Go->setText("Stop");
    }
}
void MainWindow::timerSlot()
{
    uchar dir = 0;
    if(ui->DIR0->isChecked())
    {
        dir = 0;
    }
    else
    {
        dir = 1;
    }
    uchar checksum = 0;
    Trans[0] = 0xaa;
    Trans[1] = 0xbb;
    Trans[2] = 0x0c;
    Trans[3] = 0x01;
    Trans[4] = dir;
    Trans[5] = 0x00;
    Trans[6] = 0x00;
    Trans[7] = 0x00;
    Trans[8] = 0x0a;
    Trans[9] = 0x0;
    Trans[10] = 0xcc;
    Trans[11] = 0xdd;
    checksum = Checksum(Trans,Trans[2]);
    Trans[9] = checksum;
    serial1->write(reinterpret_cast<char*>(Trans),Trans[2]);
}


void MainWindow::refreshSerial()
{
    ui->comboBox->clear();
    ui->comboBox->addItems(getSerialPortNames());
}

QStringList MainWindow::getSerialPortNames()
{
    QStringList slist;
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
        //检测是否可用
        if(!info.isBusy())
            slist<<info.portName();
    }
    if(slist.isEmpty()){
        qDebug()<<"未找到可用串口,请确认串口连接正常后点击刷新";
    }
    return slist;
}
